//
//  CPDiscoverSearchView.swift
//  WeiBo1
//
//  Created by chengpeng on 16/3/4.
//  Copyright © 2016年 chengpeng. All rights reserved.
//

import UIKit

class CPDiscoverSearchView: UIView {

    //右边约束
    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
   //取消按钮
    @IBOutlet weak var CancelButton: UIButton!

    //输入框
    @IBOutlet weak var textfield: UITextField!
    
    class func searchView() -> CPDiscoverSearchView {
        
        //从 mainBundle里面加载View
        return NSBundle.mainBundle().loadNibNamed("CPDiscoverSearchView", owner: nil, options: nil).last! as! CPDiscoverSearchView
        
        
        
        
    }
    
    @IBAction func EditingDidBegin(sender: UITextField) {
        print("ssss")
        
        //调整右边约束
        textFieldRightCons.constant =  self.CancelButton.frame.width

        UIView.animateWithDuration(0.25) { () -> Void in
            self.textfield.layoutIfNeeded()
        }
        
    }
    
    
    override func awakeFromNib() {
        
        
        leftView.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        textfield.leftView = leftView
        textfield.leftViewMode = .Always
        
        //设置背景色
        textfield.backgroundColor = UIColor.blueColor()
        //设置边框宽度
        textfield.layer.borderWidth = 2
        //设置边框颜色
        textfield.layer.borderColor = UIColor.redColor().CGColor
        //设置圆角弧度
        textfield.layer.cornerRadius = 5
        textfield.layer.masksToBounds = true
        
        
        
        
    }
    
    @IBAction func cancelButtonClick(sender: UIButton) {
        
        //1.调整右边约束
        textFieldRightCons.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textfield.layoutIfNeeded()
        }
        
        
        //2.取消 textfield 的第一响应者
        textfield.resignFirstResponder()
        
    }
    
    
    //MARK -- 懒加载控件
    private lazy var leftView : UIImageView = {
        
        let view = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        view.contentMode = .Center
        return view
    
    }()
    
}
